package com.example.gkcircle.guide;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.MainActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.utils.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jehu on 16/4/11.
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener{

    private ViewPager vp;
    private GuideViewPagerAdapter mAdapter;
    private List<View> views;
    private ImageView[] dots;
    public static boolean isFinishRegister;
    private int currentIndex;


    @Override
    protected void initView() {
        setContentView(R.layout.guide_activity);
        LayoutInflater inflater = LayoutInflater.from(this);
        views = new ArrayList<View>();
        views.add(inflater.inflate(R.layout.guide_one_fragment, null));
        views.add(inflater.inflate(R.layout.guide_two_fragment, null));
        views.add(inflater.inflate(R.layout.guide_three_fragment, null));
        views.add(inflater.inflate(R.layout.guide_four_fragment, null));
        mAdapter = new GuideViewPagerAdapter(views, this);
        vp = (ViewPager) findViewById(R.id.guide_viewpager);
        vp.setAdapter(mAdapter);
        vp.setOnPageChangeListener(this);
        //初始化下页的点
        initDots();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void setData() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrentDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void setCurrentDot(int position) {
        if (position < 0 || position > views.size() - 1
                || currentIndex == position) {
            return;
        }

        dots[position].setImageResource(R.mipmap.dot_pressed);
        dots[currentIndex].setImageResource(R.mipmap.dot_normal);

        currentIndex = position;
    }

    private void initDots() {

        LinearLayout ll = (LinearLayout) findViewById(R.id.guide_ll);

        dots = new ImageView[views.size()];

        for (int i = 0; i < views.size(); i++) {
            dots[i] = (ImageView) ll.getChildAt(i);
            dots[i].setImageResource(R.mipmap.dot_normal);
        }

        currentIndex = 0;
        dots[currentIndex].setImageResource(R.mipmap.dot_pressed);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            this.overridePendingTransition(0, R.anim.zoom_in);
            ActivityUtil.startActivity(this, MainActivity.class);
            finish();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(GuideActivity.isFinishRegister){
            this.overridePendingTransition(0, R.anim.zoom_in);
            ActivityUtil.startActivity(this, MainActivity.class);
            finish();
        }
    }
}
